# -*- coding: utf-8 -*-
import datetime, re

from peewee import *
from basemodel import BaseModel


class Post(BaseModel):
    title = CharField()
    content = TextField()
    keyword = CharField()
    create_time = DateTimeField(default=datetime.datetime.now)
    published = BooleanField(default=False)
    read_count = IntegerField(default=0)
    comment_count = IntegerField(default=0)

    def description(self):
        re_style = re.compile('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>',re.I)
        re_image = re.compile('<\s*img/>')
        re_h = re.compile('</?\w+[^>]*>')
        re_ref = re.compile('&[a-zA-Z]+;')
        s = re_style.sub('', self.content)
        s = re_image.sub('', s)
        s = re_h.sub('',s)
        s = re_ref.sub('', s)
        return s[0:300]

    def thumb(self):
        re_style = re.compile('src="([^"]*)"')
        r = re_style.findall(self.content)
        if len(r) > 0:
            return re_style.findall(self.content)[0]+'?imageMogr2/thumbnail/80x'
        else:
            return ''
